﻿var car = new TripDetails.Vehicle("кола", "форд", 1995, 5, 9);
var driver = new TripDetails.Driver("Mitio", "Pishtova", "krisiI@abv.bg", "+359886", 18, "m", car, "10", "no smoking!");
var trip = new TripDetails.Trip("София", "Варна", 320, "12.04.2013");
var price = new TripDetails.Price(3, 'лв.');

var driver2 = new TripDetails.Driver("Kolio", "Pishtova", "kolio@abv.bg", "+359888", 18, "m", car, "10", "no smoking!");
var trip2 = new TripDetails.Trip("София", "Варна", 550, "12.04.2013 10:00");
var price2 = new TripDetails.Price(6, 'лв.');

var offer = new TripDetails.Offer(driver, price, trip);
var offer2 = new TripDetails.Offer(driver2, price2, trip2);

var OfferModel = function (offers) {
    var self = this;
    self.offers = ko.observableArray(offers);

    self.addOffer = function () {
        self.offers.push({
            driver: "",
            price: "",
            trip: "",
        });
    };

    self.removeOffer = function (offer) {
        self.offers.remove(offer);
    };

    self.printOffer = function (offer) {
        var div = document.createElement('div');
        div.innerHTML = offer.toString();
        document.body.appendChild(div);
    }
    self.save = function (form) {
        alert("Could now transmit to server: " + ko.utils.stringifyJson(self.offers));
        // To actually transmit to server as a regular form post, write this: ko.utils.postJson($("form")[0], self.offers);
    };
};

var get = new dbModule.Get();

get.allOffers(function (data) {
    var allOffers = [];
    for (var i = 0; i < data.length; i++) {
        var obj = new TripDetails.Offer(data[i].driver, data[i].price, data[i].trip);
        JSON.stringify(obj);
        allOffers.push(obj);
    }
    var viewModel = new OfferModel(allOffers);

    ko.applyBindings(viewModel);

});
